package com.controller;

import com.model.Order;
import com.model.User;
import com.service.UserService;
import com.service.User_OrderService;
import com.tool.MailService;
import com.vo.LoginVO;
import com.vo.RegisterVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MailService mailService;

    @Autowired
    private User_OrderService user_orderService;

//    @RequestMapping("/")
//    public String getUser(){
//        return "index";
//    }
//
//    @RequestMapping("/new")
//    public String getUser(Model model){
//        List<User> userList = userService.getUserList();
//        model.addAttribute("userList",userList);
//        return "index";
//    }
//
//    @PostMapping("/login")
//    public String login(LoginVO loginVO){
////        User userByemail = userService.getUserByemail(loginVO.getEmail());
////        if(userByemail.getPassword().equals(loginVO.getPassword())){
////            return "/index.html";
////        }
////        return "/registertest.html";
//        User login = userService.login(loginVO.getEmail(), loginVO.getPassword());
//        if(login!=null) return "index";
//        else return "login";
//    }

    /**
     * @TODO 错误处理，是否能直接根据数据库异常？
     * @return DUPLICATE重复  SECCUSS成功   ERROR错误
     * */
    @RequestMapping("/register")
    @ResponseBody
    public String register(@RequestBody RegisterVO registerVO,HttpServletRequest httpServletRequest){
        boolean valid = userService.UserRegisterValidate(registerVO.getEmail());
        if(!valid) return "DUPLICATE";
        boolean status = userService.registerUser(registerVO);
        if(!status) return "ERROR?";
        String email=(String)httpServletRequest.getSession().getAttribute("email");
        int vcode =(int) httpServletRequest.getSession().getAttribute("vcode");
        if(registerVO.getEmail().equals(email)&&registerVO.getVcode()==vcode){
            return "SECCUSS";
        }
        else return "ERROR";
    }

    /**
     * @param email
     * */
    @RequestMapping("/vcode")
    @ResponseBody
    public boolean vcode(@RequestBody Map<String,String> email, HttpServletRequest httpServletRequest){
        String useremail= email.get("email");
        mailService.setAddress("cc5281@126.com",useremail, "注册码");
        mailService.send("smtp.126.com", "cc5281@126.com", "BLVJCVTCHDECUIRG");
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("email",useremail);
        session.setAttribute("vcode",mailService.getVCode());
        return true;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public boolean login(LoginVO loginVO,HttpServletRequest httpServletRequest){
        User loginRes = userService.login(loginVO.getEmail(), loginVO.getPassword());
        if(loginRes==null) return false;
        else {
            httpServletRequest.getSession().setAttribute("userid",loginRes.getId());
            return true;
        }
    }

    @RequestMapping(value = "/order",method = RequestMethod.POST)
    @ResponseBody
    public List<Order> gerOrderByUserId(HttpServletRequest httpServletRequest){
        Long userid= (Long) httpServletRequest.getSession().getAttribute("userid");
        List<Order> orderByUserId = user_orderService.getOrderByUserId(userid);
        return orderByUserId;
    }
}
